import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/res/colors.dart';
import 'package:base_flutter/base_flutter.dart';
import 'package:oktoast/oktoast.dart';

import '../../a_yyt/models/address_book_entity.dart';
import '../../util/utils.dart';

class SearchAddressBookPage extends StatefulWidget{
  List<AddressBookSection> list;

  SearchAddressBookPage({required this.list});

  @override
  State<StatefulWidget> createState() => _SearchAddressBookState();

}

class _SearchAddressBookState extends State<SearchAddressBookPage>{

  List<AddressBookInfo> searchedList = [];

  List<AddressBookInfo> totalList = [];

  @override
  void initState() {
    super.initState();
    for(var item in widget.list){
      totalList.addAll(item.staffPhoneBooks??[]);
    }
  }

  void searchBook(String words){
    List<AddressBookInfo> list = [];
    // searchedList.clear();
    if(!words.isNullOrEmpty()){
      for(var item in totalList){
        if((item.name??"").contains(words)){
          list.add(item);
        }
      }
    }
    setState(() {
      searchedList = list;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Row(
          children: [
            SearchBar(hintText: "请输入姓名搜索",
              shadowColor: WidgetStateProperty.all(Colors.transparent),
              backgroundColor: WidgetStateProperty.all(Colours.bg_color),
              textInputAction: TextInputAction.search,
              textCapitalization: TextCapitalization.characters,
              leading: Icon(Icons.search),
              onChanged: (str){
               searchBook(str);
              },
              padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: 15)),).setWeight(1),
            // TextButton(onPressed: (){}, child: Text('搜索'))
          ],
        ).addToContainer(
          height: 46
        ),
      ),
      body: ListView.separated(itemBuilder: (context,index){
        var e = searchedList[index];
        return Row(
          children: [
            SizedBox(width: 16,),
            CommonText(e.nickName,textColor: Colors.white,textSize: 18,fontWeight: FontWeight.bold).addToContainer(
                width: 60,height: 60,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    gradient: LinearGradient(colors: [
                      Colors.blue,Colors.blue[400]!,Colors.blue[200]!
                    ],begin: Alignment.bottomRight,end: Alignment.topLeft),
                    borderRadius: 8.borderRadius
                )
            ),
            SizedBox(width: 20,),
            Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    CommonText(e.name),
                    SizedBox(width: 10,),
                    CommonText(e.officeName??"其他",textColor: Colors.white,textSize: 14).addToContainer(
                      decoration: BoxDecoration(
                        color: Colors.blue,
                        borderRadius: 4.borderRadius
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 6,vertical: 2)
                    )
                  ],
                ),
                SizedBox(height: 13,),
                CommonText(e.mobile)
              ],
            ).setWeight(1),
            Column(
              children: [
                Icon(Icons.phone_forwarded_rounded,color: Colors.white,size: 20,).addToContainer(
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: Colors.blue
                    ),
                    width: 30,height: 30
                ).onTap(() {
                  showBottomSelect(context, items: ['拨打电话','发送短信','复制号码'],onSelect: (item){
                    if(item=='拨打电话'){
                      Utils.launchTelURL(e.mobile??"");
                    }
                    if(item=='发送短信'){
                      Utils.launchSmsURL(e.mobile??"");
                    }
                    if(item=='复制号码'){
                      if(!e.mobile.isNullOrEmpty()){
                        Utils.copyToClipboard(e.mobile??"");
                        showToast("已复制到剪切板",context: context);
                      }
                    }
                  });

                }),
              ],
            ),
            SizedBox(width: 16,),
          ],
        );
      }, separatorBuilder: (context,index){
        return SizedBox(height: 15,);
      }, itemCount: searchedList.length,padding: EdgeInsets.symmetric(vertical: 16),),

    );
  }

}